//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Web.UI.HtmlControls;
namespace DotNetNuke.UI.UserControls
{
	public class SectionHeadControl : System.Web.UI.UserControl
	{
		protected System.Web.UI.WebControls.Label lblTitle;
		protected System.Web.UI.WebControls.ImageButton imgIcon;
		protected System.Web.UI.WebControls.Panel pnlRule;
		private bool _includeRule = false;
		private bool _isExpanded = true;
		private string _javaScript = "__dnn_SectionMaxMin";
		private string _maxImageUrl = "images/plus.gif";
		private string _minImageUrl = "images/minus.gif";
		private string _resourceKey;
		private string _section;
		public string CssClass
		{
			get { return lblTitle.CssClass; }
			set { lblTitle.CssClass = value; }
		}
		public bool IncludeRule
		{
			get { return _includeRule; }
			set { _includeRule = value; }
		}
		public bool IsExpanded
		{
			get { return DotNetNuke.UI.Utilities.DNNClientAPI.MinMaxContentVisibile(imgIcon, -1, !_isExpanded, Utilities.DNNClientAPI.MinMaxPersistanceType.Page); }
			set
			{
				_isExpanded = value;
				DotNetNuke.UI.Utilities.DNNClientAPI.MinMaxContentVisibile(imgIcon, -1, !_isExpanded, Utilities.DNNClientAPI.MinMaxPersistanceType.Page, value);
			}
		}
		public string JavaScript
		{
			get { return _javaScript; }
			set { _javaScript = value; }
		}
		public string MaxImageUrl
		{
			get { return _maxImageUrl; }
			set { _maxImageUrl = value; }
		}
		public string MinImageUrl
		{
			get { return _minImageUrl; }
			set { _minImageUrl = value; }
		}
		public string ResourceKey
		{
			get { return _resourceKey; }
			set { _resourceKey = value; }
		}
		public string Section
		{
			get { return _section; }
			set { _section = value; }
		}
		public string Text
		{
			get { return lblTitle.Text; }
			set { lblTitle.Text = value; }
		}
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			try
			{
				if (!String.IsNullOrEmpty(ResourceKey))
				{
					lblTitle.Attributes["resourcekey"] = ResourceKey;
				}
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);

			try
			{
				HtmlControl ctl = (HtmlControl)this.Parent.FindControl(Section);
				if (ctl != null)
				{
					DotNetNuke.UI.Utilities.DNNClientAPI.EnableMinMax(imgIcon, ctl, !IsExpanded, Page.ResolveUrl(MinImageUrl), Page.ResolveUrl(MaxImageUrl), Utilities.DNNClientAPI.MinMaxPersistanceType.Page);
				}
				pnlRule.Visible = _includeRule;
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		protected void imgIcon_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			HtmlControl ctl = (HtmlControl)this.Parent.FindControl(Section);
			if (ctl != null)
			{
				this.IsExpanded = !this.IsExpanded;
			}
		}
	}
}
